/*********************************************************************************
 *      Copyright:  (C) 2020 YangZhengQing<2489833081@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  get_time.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/05/2020)
 *         Author:  YangZhengQing <2489833081@qq.com>
 *      ChangeLog:  1, Release initial version on "04/05/2020 06:30:02 PM"
 *                 
 ********************************************************************************/





#include "../include/type.h"
#include "../include/client.h"

/*
int main (int argc, char *argv[])
{
 
    char                mytime[20];
    int                 rv = -1;//函数返回值

    memset(mytime,0,sizeof(mytime));
    rv = get_date_time(mytime);
    if(rv < 0)
    {
        return 0;
    }

    printf("当前时间为:%s\n",mytime);

    return 0;
} 
*/

/*功能：获取当前时间和日期
 * 参数：buf：传出日期和时间的缓冲区
 * 返回值：成功返回0，失败返回-1*/
int get_date_time(pack_t *pp)
{
    time_t                      nSeconds;
    struct tm                   *pTM;
    int                         rv = -1;
    rv = time(&nSeconds);
    if(rv < 0)
    {
        printf("time failed:%s\n",strerror(errno));
        return -1;
    }

    pTM = localtime(&nSeconds);
    if(!pTM)
    {
        printf("Local time failed:%s\n",strerror(errno));
        return -1;
    }

    /*  系统日期和时间,格式: yyyymmddHHMMSS */
    sprintf(pp->s_pack.date, "%04d-%02d-%02d",pTM->tm_year + 1900, pTM->tm_mon + 1, pTM->tm_mday);

    sprintf(pp->s_pack.time,"%02d:%02d:%02d",pTM->tm_hour, pTM->tm_min, pTM->tm_sec);


    return 0;
}
